<template>
  <div id="app" class="container">
    <div class="form-wrapper">
      <h2 class="title">获取公钥</h2>
      <div class="form-group">
        <label for="username">用户名</label>
        <input type="text" id="username" v-model="username" placeholder="请输入用户名">
      </div>
      <button class="btn" @click="getPublicKey">获取公钥</button>
      <div v-if="publicKey" class="public-key-display">
        <label>公钥:</label>
        <textarea readonly v-html="publicKey"></textarea>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
export default {
  data() {
    return {
      username: '',
      publicKey: '', 
    };
  },
  methods: {
    async getPublicKey() {
      try {
        const response = await axios.post('http://192.168.124.128:8081/api/getPublickey', { username: this.username });
        if (response.data) {
          this.publicKey = response.data.resolvedKey; 
        } else {
          alert('公钥获取失败');
        }
      } catch (error) {
        console.error('There was an error!', error);
        alert('请求失败');
      }
    },
  },
};
</script>


<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 90vh;
  background-color: #f7f7f7;
}

.form-wrapper {
  padding: 20px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  width: 300px;
}

.title {
  text-align: center;
  color: #333;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  color: #666;
}

.form-group input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-sizing: border-box;
}

.btn {
  display: block;
  width: 100%;
  padding: 10px;
  border: none;
  background-color: #007bff;
  color: white;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.btn:hover {
  background-color: #0056b3;
}
.public-key-display {
  margin-top: 15px;
}

.public-key-display textarea {
  width: 100%;
  height: 100px;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-sizing: border-box;
  resize: none;
}
</style>